//ThrowTest.java
package com.ylaihui.exception;

public class ThrowTest {
	public static void main(String[] args) {
		try {
			Student stu = new Student();
			stu.register(-1000);
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}

class Student{
	int id;
	public void register(int id) throws Exception{
		if(id > 0)
			this.id = id;
		else
//			System.out.println("id is not valid!");
			// 此时不需要 try-catch，运行时异常不包含编译时异常
//			throw new RuntimeException("id is not valid!");
			// 此时需要 try-catch， Exception包含编译时异常，编译时可能不通过，需要处理
			throw new Exception("id is not valid!");
	}
}
